package cn.utils;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;

public class ObjectUtil {

	/* 
     * 
     * 类定义中child一定要extends father； 
     * 而且child和father一定为严格javabean写法，属性为deleteDate，方法为getDeleteDate 
     */  
	public static <T extends Serializable> T copyProperty(Object from,T to) throws Exception{
		
        Class fatherClass= from.getClass();  
        Class childClass=to.getClass();
        Field fields[]= fatherClass.getDeclaredFields();  
        for(int i=0;i<fields.length;i++){  
        	try {
        		 Field field=fields[i];//取出每一个属性，如deleteDate  
        		 field.setAccessible(true);
        		 Class  type=field.getType();
                 Method getMethod=fatherClass.getDeclaredMethod("get"+StringUtil.toUpperFirstCase(field.getName()));//方法getDeleteDate  
                 Object obj=getMethod.invoke(from);//取出属性值         
                 
                 Method setMethod=childClass.getDeclaredMethod("set"+StringUtil.toUpperFirstCase(field.getName()),type);//方法getDeleteDate  
                 setMethod.setAccessible(true);
                 setMethod.invoke(to,obj);
                 
//                 System.out.println(child.getClass().getMethod("get"+StringUtil.toUpperFirstCase(field.getName())).invoke(child));
			} catch (Exception e) {
			}
           
        }  
		
		return (T)to;
	}
	/**
	 * 从一个实体把具有相同属性的值负值到另外一个实体
	 * @param from  
	 * @param to
	 * @return
	 * @throws Exception
	 */
    public static  <T extends Serializable> T copyProperties(Object from,T to) throws Exception{
		
        Class fatherClass= from.getClass();  
        Class childClass=to.getClass();
        Field fields[]= fatherClass.getDeclaredFields();  
        for(int i=0;i<fields.length;i++){  
        	try {
        		 Field field=fields[i];//取出每一个属性，如deleteDate  
        		 field.setAccessible(true);
        		 Class  type=field.getType();
                 Method getMethod=fatherClass.getDeclaredMethod("get"+StringUtil.toUpperFirstCase(field.getName()));//方法getDeleteDate  
                 Object obj=getMethod.invoke(from);//取出属性值         
                 
                 if(null!=obj){
                 Method setMethod=childClass.getDeclaredMethod("set"+StringUtil.toUpperFirstCase(field.getName()),type);//方法getDeleteDate  
                 setMethod.setAccessible(true);
                 setMethod.invoke(to,obj);
                 }
                 
//                 System.out.println(child.getClass().getMethod("get"+StringUtil.toUpperFirstCase(field.getName())).invoke(child));
			} catch (Exception e) {
			}
           
        }  
		
		return to;
	}
    
    /**
     * 把数组指定的属性拷贝值
     * @param from
     * @param to
     * @param proerties 指定拷贝属性
     * @return
     * @throws Exception
     */
   public static <T extends Serializable> T copyProperties(Object from,T to,String [] proerties) throws Exception{
		
        Class fatherClass= from.getClass();  
        Class childClass=to.getClass();
        for(int i=0;i<proerties.length;i++){  
        	try {
        		 Field field=fatherClass.getDeclaredField(proerties[i]);
        		 field.setAccessible(true);
        		 Class  type=field.getType();
                 Method getMethod=fatherClass.getDeclaredMethod("get"+StringUtil.toUpperFirstCase(proerties[i]));//方法getDeleteDate  
                 Object obj=getMethod.invoke(from);//取出属性值         
                 
                 if(null!=obj){
                 Method setMethod=childClass.getDeclaredMethod("set"+StringUtil.toUpperFirstCase(proerties[i]),type);//方法getDeleteDate  
                 setMethod.setAccessible(true);
                 setMethod.invoke(to,obj);
                 }
                 
//                 System.out.println(child.getClass().getMethod("get"+StringUtil.toUpperFirstCase(field.getName())).invoke(child));
			} catch (Exception e) {
			}
           
        }  
		
		return to;
	}
    
    
    
    
    /**
     * 打印两个实体中相同的属性
     * @param obj1
     * @param obj2
     */
    public  static void printCommonPropertie(Object obj1,Object obj2,String include){
    	Field fields1[]= obj1.getClass().getDeclaredFields();  
    	Field fields2[]= obj2.getClass().getDeclaredFields();
    	
    	String  result="";
    	
    	for(Field f1:fields1){
    		for(Field f2:fields2){
    			if(f1.getName().equals(f2.getName())){
    				result=result+","+include+f2.getName()+include;
    			}
    		}
    	}
    	
    	System.out.println(result);
    }
    
    
    /**
     * 通过反射获取属性值
     * @param obj
     * @param propertyName
     * @return
     * @throws NoSuchMethodException 
     * @throws SecurityException 
     */
    public static Object getProperty(Object obj,String propertyName) throws Exception{
    	Class clazz=obj.getClass();
    	Method getMethod=clazz.getDeclaredMethod("get"+StringUtil.toUpperFirstCase(propertyName));//方法getDeleteDate  
        Object pVal=getMethod.invoke(obj);//取出属性值         
        return pVal;
    }
    
    /**
     * 通过反射对属性值负值
     * @param obj  实体类
     * @param propertyName 属性名称
     * @param val 属性值
     * @return
     * @throws NoSuchMethodException 
     * @throws SecurityException 
     */
    public static Object setProperty(Object obj,String propertyName,Object val) throws Exception{
    	
    	 Class clazz=obj.getClass();
    	 Method getMethod=clazz.getDeclaredMethod("get"+StringUtil.toUpperFirstCase(propertyName));//方法getDeleteDate  
    	 Class type=getMethod.getReturnType();
    	 if(null!=val){
        	String typeStr=type.toString();
        	if(typeStr.contains("String")){
        		if(null!=val)
        		val=val.toString().trim();
        	}else if(typeStr.contains("Date")){
        		if(StringUtil.isAllEmpty(val.toString())){
        			val=null;
        		}
        	}else if(typeStr.contains("Integer")||typeStr.contains("int")){
        		if(StringUtil.isAllEmpty(val.toString())){
        			val=null;
        		}else{
        			val=Integer.parseInt(val.toString());
        		}
        	}else if (typeStr.contains("Long")||typeStr.contains("long")){
        		if(StringUtil.isAllEmpty(val.toString())){
        			val=null;
        		}else{
        			val=Long.parseLong(val.toString());
        		}
        	}else if(typeStr.contains("Double")||typeStr.contains("double")){
        		if(StringUtil.isAllEmpty(val.toString())){
        			val=null;
        		}else{
        			val=Double.parseDouble(val.toString());
        		}
        	}
    	 }
        	try {
            	Method setMethod=clazz.getDeclaredMethod("set"+StringUtil.toUpperFirstCase(propertyName),type);//方法getDeleteDate 
                setMethod.setAccessible(true);
                setMethod.invoke(obj,val);//取出属性值         
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
        	
    	
    	return obj;
    	
    }
    

}
